package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.admin.dtos.AdminUserDto;
import com.heima.model.admin.pojos.AdminUserPojo;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.user.mapper.AdminUserMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.AdminUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Slf4j
@Transactional
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUserPojo> implements AdminUserService {

    @Autowired
    private AdminUserMapper adminUserMapper;

    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private IWemediaClient iWemediaClient;

    /**
     * 查询用户列表
     *
     * @return
     */
    @Override
    public ResponseResult findList(AdminUserDto dto) {
//        1.校验页数
        dto.checkParam();
//        2.校验参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
//        3.查询
//        3.1分页条件查询
        IPage page = new Page(dto.getPage(), dto.getSize());
//        3.2创建条件构造器
//        QueryWrapper<AdminUserPojo> wrapper = new QueryWrapper();
        LambdaQueryWrapper<AdminUserPojo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        3.3按照id查询
        if (dto.getId() != null) {

//            wrapper.eq("id", dto.getId());
            lambdaQueryWrapper.eq(AdminUserPojo::getId, dto.getId());
        }
//        3.3按照状态查询
        if (dto.getStatus() != null) {

//            wrapper.eq("status", dto.getStatus());
            lambdaQueryWrapper.eq(AdminUserPojo::getStatus, dto.getStatus());

        }
//        3.4按照时间倒序查询
//        wrapper.orderByDesc("createdTime");
        lambdaQueryWrapper.orderByDesc(AdminUserPojo::getCreatedTime);

//        IPage page1 = adminUserMapper.selectPage(page, wrapper);
        IPage page1 = page(page, lambdaQueryWrapper);
        //3.结果返回
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page1.getRecords());
        return responseResult;
    }

    /**
     * 审核通过或驳回
     */
    @Override
    public ResponseResult successOrFail(AdminUserDto dto) {
//        1.校验id
        if (dto.getId() == null || dto.getId() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
//        2.ById查询
        Integer id = dto.getId();
        AdminUserPojo adminUserPojo = adminUserMapper.selectById(id);
////        3.改变用户状态
//        adminUserPojo.setStatus((short) 9);
//        4.刷新到数据库
        //adminUserMapper.insert(adminUserPojo);
        updateById(adminUserPojo);
        ApUser user = null;
        if (dto.getStatus() == 9) {
//            5.远程调用修改自媒体人状态

            user = getUser(adminUserPojo);
            user.setFlag((short) 1);
            apUserMapper.updateById(user);
            iWemediaClient.updateStatus(user);
            adminUserPojo.setStatus(Short.parseShort(String.valueOf(dto.getStatus())));
        }
        if (dto.getStatus() == 2) {
            adminUserPojo.setReason(dto.getMsg());
            adminUserPojo.setStatus(Short.parseShort(String.valueOf(dto.getStatus())));
        }
        adminUserMapper.updateById(adminUserPojo);
//        6.返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    public ApUser getUser(AdminUserPojo pojo) {
        Integer userId = pojo.getUserId();
        ApUser apUser = apUserMapper.selectById(userId);
        return apUser;

    }
//
//    /**
//     * 审核驳回
//     */
//    @Override
//    public ResponseResult fail(AdminUserDto dto) {
////        1.校验id
//        if (dto.getId() == null || dto.getId() == 0) {
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
//        }
////        2.ById查询
//        Integer id = dto.getId();
//        AdminUserPojo adminUserPojo = adminUserMapper.selectById(id);
////        3.改变用户状态
//        adminUserPojo.setStatus(Short.parseShort(String.valueOf(dto.getStatus())));
////        4.刷新到数据库
//        adminUserMapper.insert(adminUserPojo);
////        5.远程调用修改自媒体人状态
//       // updateStatus();//todo
////        6.返回结果
//        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
//    }
}
